package work.tree;

import work.TreeNode;

public class Problem230 {
    int count;

    public int kthSmallest(TreeNode root, int k) {
        this.count = k;
        int ans = dfs(root);
        return ans;
    }

    public int dfs(TreeNode root) {
        if (root == null) return -1;
        //左
        int leftAns = dfs(root.left);
        if (leftAns != -1) return leftAns;
        //中
        if (--count == 0) return root.val;
//        右
        int rightAns = dfs(root.right);
        return rightAns;

    }


}
